
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import 'dd_core/dd_config.dart';
import 'dd_core/dd_hive_service.dart';
import 'dd_core/dd_ios_pay_service.dart';
import 'dd_core/dd_log_service.dart';
import 'dd_core/dd_other.dart';
import 'dd_core/dd_user_service.dart';

void main() async {
   await HiveService.init();
   await DDAppInfo.getInstance().init();
   setLogLauncherId();
   runApp(MyApp());
}

void setLogLauncherId(){
  DDLogService().setLauncherId();
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      enableLog: false,
      theme: ThemeData(
        primarySwatch: Colors.lightBlue,
        highlightColor: Colors.transparent,
        splashColor: Colors.transparent,
      ),
      initialRoute: DDRoutesConfig.splash,
      //initialRoute: DDRoutesConfig.guide,
      debugShowCheckedModeBanner: false,
      routes: DDRoutesConfig.routes,
      onInit: () async {
        appServerInit();
      },
      navigatorObservers: [
        MyObserver(),
      ],
      onReady: () {},
      builder: EasyLoading.init(builder: (ctx, w) {
        return MaxScaleTextWidget(child: w);
      }),
    );
  }

  void appServerInit() {
    Get.lazyPut(() => AppService());
    Get.lazyPut(() => DDUserService());
    Get.lazyPut(() => DDIosPayService());
    //Get.lazyPut(() => DDRongcloudService());

    EasyLoading.instance
      ..userInteractions = false
      ..indicatorType = EasyLoadingIndicatorType.circle
      ..indicatorSize = 50
      ..boxShadow = <BoxShadow>[]
      ..backgroundColor = Colors.white.withOpacity(0.9)
      ..textColor = Colors.black;
  }
}

class MaxScaleTextWidget extends StatelessWidget {
  final Widget? child;

  const MaxScaleTextWidget({
    Key? key,
    this.child,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final data = MediaQuery.of(context);
    return MediaQuery(
      data: data.copyWith(textScaleFactor: 1, boldText: false),
      child: child ?? SizedBox(),
    );
  }
}

class MyObserver extends NavigatorObserver {
  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didPush(route, previousRoute);
    if (route is MaterialPageRoute || route is GetPageRoute) {
      Future.delayed(Duration(milliseconds: 100), () {
        Get.find<AppService>().routeNameChange(Get.routing.current);
      });
    }
  }

  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didPop(route, previousRoute);
    if (route is MaterialPageRoute || route is GetPageRoute) {
      Future.delayed(Duration(milliseconds: 100), () {
        Get.find<AppService>().routeNameChange(Get.routing.current);
      });
    }
  }
}

